package com.whut.whw.personalinfo;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.util.GlideCircularLoadUtils;

import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

import static android.app.Activity.RESULT_OK;


/**
 * Created by 曾旺红 on 2017/06/16.
 */

public class PsnDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private List<Object> list;

    public static final int TYPE_HEADER=0;
    public static final int TYPE_CONTENT=1;
    public static final int REQUEST_IMAGE=100;

    public static Context mcontext;

    public PsnDataAdapter (List<Object> list){
        this.list=list;
    }

    //点击
    private OnItemClickListener mOnItemClickListener;
    //长按
    private OnItemLongClickListener mOnItemLongClickListener;
    //点击
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    //长按
    public void setmOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
    //点击监听接口
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    //长按监听接口
    public interface OnItemLongClickListener{
        void onItemLongClick(View view, int position);
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder{
        LinearLayout headPhoto;
        ImageView dataPhoto;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            dataPhoto=(ImageView)itemView.findViewById(R.id.data_photo);
            headPhoto=(LinearLayout)itemView.findViewById(R.id.head_photo);
//            //设置监听
//            headPhoto.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(mcontext, MultiImageSelectorActivity.class);
//                    // 是否显示相机
//                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
//                    // 最大选择图像量
//                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
//                    // 选择模式 (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
//                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
//                    // 默认选择图像（支持数组列表）
//                    //intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, defaultDataArray);
//                    ((PsnDataActivity)mcontext).startActivityForResult(intent, REQUEST_IMAGE);
//                }
//            });
        }

    }

    public static class ContentViewHolder extends RecyclerView.ViewHolder{
        TextView dataName;
        TextView dataContent;
        public ContentViewHolder(View itemView) {
            super(itemView);
            dataName=(TextView)itemView.findViewById(R.id.data_name);
            dataContent=(TextView)itemView.findViewById(R.id.data_content);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mcontext=parent.getContext();
        if(viewType==TYPE_HEADER){
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_psndataheader,parent,false);
            RecyclerView.ViewHolder holder=new HeaderViewHolder(view);
            return holder;
        }
        else if(viewType==TYPE_CONTENT)
        {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_psndata_item,parent,false);
            RecyclerView.ViewHolder holder=new ContentViewHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0)
            return TYPE_HEADER;
        else
            return TYPE_CONTENT;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof HeaderViewHolder)
        {
            Object photo=list.get(position);
            if(photo.toString().indexOf("http") != -1) {
                Glide
                        .with(mcontext)
                        .load(photo.toString())
                        .asBitmap().centerCrop()
                        .into(new GlideCircularLoadUtils(mcontext,((HeaderViewHolder)holder).dataPhoto));
            }
            else{
                Glide
                        .with(mcontext)
                        .load((int)photo)
                        .asBitmap().centerCrop()
                        .into(new GlideCircularLoadUtils(mcontext,((HeaderViewHolder)holder).dataPhoto));
            }

        }
        else if(holder instanceof ContentViewHolder){
            PsnDataModel psnDataModel=(PsnDataModel)list.get(position);
            ((ContentViewHolder)holder).dataName.setText(psnDataModel.getName());
            ((ContentViewHolder)holder).dataContent.setText(psnDataModel.getContent());
        }

        //判断是否设置了监听器
        //单击判断
        if(mOnItemClickListener != null){
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
        //长按判断
        if(mOnItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView,position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

}
